﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP_RORZE_DLL.Product.DAO;
using ERP_RORZE_DLL.Product.database;
using ERP_RORZE_PERMISSION.Base;
using ERP_RORZE_PRODUCT.Product.DAO;

namespace ERP_RORZE_PRODUCT.Product.Machine
{
    public partial class frm_MachineConfigEdit : DevExpress.XtraEditors.XtraForm
    {
        ClsMachineDAO clsmachineDAO = new ClsMachineDAO();
        ClsDataSheetTable b = new ClsDataSheetTable();
        DataTable dt;
        public event EventHandler Reload;
        ClsIncauhinhDAO clsDAO = new ClsIncauhinhDAO();
        private LogUtils log;
        ClsWTSDAO clsWTSDAO = new ClsWTSDAO();

        public string ID;
        public frm_MachineConfigEdit()
        {
            InitializeComponent();
            log = new LogUtils();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frm_MachineConfigEdit_Load(object sender, EventArgs e)
        {
            // lấy danh sách kiểu chi tiết
            DataTable dt0 = new DataTable();
            dt0 = clsDAO.GetAllKHCT();
            DataRow r0 = dt0.NewRow();
            r0["TenKH"] = "";
            r0["KH"] = "0";

            dt0.Rows.InsertAt(r0, 0);
            cbCode.DataSource = dt0;
            cbCode.DisplayMember = "TenKH";
            cbCode.ValueMember = "KH";

            //lấy danh sách nhân viên
            DataTable dt1 = new DataTable();
            dt1 = clsWTSDAO.GetAllStaff();
            DataRow r1 = dt1.NewRow();
            r1["FullName"] = "";
            r1["StaffID"] = "";
            dt1.Rows.InsertAt(r1, 0);
            cbStaffRegister.DataSource = dt1;
            cbStaffRegister.DisplayMember = "FullName";
            cbStaffRegister.ValueMember = "StaffID";

            //Lấy hình dạng sản phẩm
            DataTable dt2 = new DataTable();
            dt2 = clsDAO.GetAllMaterialForm();
            DataRow r2 = dt2.NewRow();
            r2["ID"] = "0";
            r2["MaterialFormID"] = "";
            dt2.Rows.InsertAt(r2, 0);
            cbForm.DataSource = dt2;
            cbForm.DisplayMember = "MaterialFormID";
            cbForm.ValueMember = "ID";

            //lấy loại cấu hình
            DataTable dt3 = new DataTable();
            dt3 = clsDAO.GetAllConfigure();
            DataRow r3 = dt3.NewRow();
            r3["ConfigureID"] = "0";
            r3["ConfigureName"] = "";

            dt3.Rows.InsertAt(r3, 0);
            cbConfigureName.DataSource = dt3;
            cbConfigureName.DisplayMember = "ConfigureName";
            cbConfigureName.ValueMember = "ConfigureID";

            // lấy danh sách phôi chuẩn phòng kinh doanh
            DataTable dt4 = new DataTable();
            dt4 = clsDAO.GetAllPCPKD();
            DataRow r4 = dt4.NewRow();
            r4["PType"] = "";
            r4["ID"] = "0";

            dt4.Rows.InsertAt(r4, 0);
            cbKDID.DataSource = dt4;
            cbKDID.DisplayMember = "PType";
            cbKDID.ValueMember = "PType";

            // lấy danh sách phôi chuẩn phòng kinh doanh
            DataTable dt5 = new DataTable();
            dt5 = clsDAO.GetAllLVL();
            DataRow r5 = dt5.NewRow();
            r5["PType"] = "";
            r5["ID"] = "0";

            dt5.Rows.InsertAt(r5, 0);
            cbKDID.DataSource = dt5;
            cbKDID.DisplayMember = "PType";
            cbKDID.ValueMember = "PType";


            b.ID = int.Parse(ID);
            dt = clsmachineDAO.GetAllMachineConfigurationByID(b);
            foreach (DataRow r in dt.Rows)
            {
                if (r["code"].ToString() == "")
                {
                    cbCode.Text = "";
                }
                else
                {
                    cbCode.Text = r["code"].ToString();
                }
                if (r["ConfigureName"].ToString() == "")
                {
                    cbConfigureName.Text = "";
                }
                else
                {
                    cbConfigureName.Text = r["ConfigureName"].ToString();
                }
                if (r["Form"].ToString() == "")
                {
                    cbForm.Text = "";
                }
                else
                {
                    cbForm.Text = r["Form"].ToString();
                }
                cbKDID.Text = r["KDID"].ToString();
                txtLenght.Text = r["Length"].ToString();
                txtNumber.Text = r["number"].ToString();
                txtPageNo.Text = r["PageNo"].ToString();
                txtRessionID.Text = r["RessionID"].ToString();
                if (r["StaffRegister"].ToString() == "")
                {
                    cbStaffRegister.Text = "";
                }
                else
                {
                    cbStaffRegister.Text = r["StaffRegister"].ToString();
                }
                txtThickness.Text = r["Thickness"].ToString();
                txtWidth.Text = r["Width"].ToString();
                dateDate.Value = DateTime.Parse(r["Date"].ToString());
                checkBafu.Checked = bool.Parse(r["Bafu"].ToString());
                checkBlast30.Checked = bool.Parse(r["Blast30"].ToString());
                checkBlast60.Checked = bool.Parse(r["Blast60"].ToString());
                checkCasiras.Checked = bool.Parse(r["Casiras"].ToString());
                checkCut.Checked = bool.Parse(r["cut"].ToString());
                checkDrawn.Checked = bool.Parse(r["Drawn"].ToString());
                checkHairLine.Checked = bool.Parse(r["HairLine"].ToString());
                checkHandl.Checked = bool.Parse(r["Handl"].ToString());
                checkHelisert.Checked = bool.Parse(r["Helisert"].ToString());
                checkInLuoi.Checked = bool.Parse(r["InLuoi"].ToString());
                checkMaBong.Checked = bool.Parse(r["MaBong"].ToString());
                checkMaDen.Checked = bool.Parse(r["MaDen"].ToString());
                checkMaTrang.Checked = bool.Parse(r["MaTrang"].ToString());
                checkMigaky.Checked = bool.Parse(r["Migaky"].ToString());
                checkPalCoat.Checked = bool.Parse(r["PalCoat"].ToString());
                checkSeal.Checked = bool.Parse(r["Seal"].ToString());
                checkSerialClose.Checked = bool.Parse(r["SerialClose"].ToString());
                checkVacuumPacked.Checked = bool.Parse(r["VacuumPacked"].ToString());
                checkWashSA.Checked = bool.Parse(r["WashSA"].ToString());
                txtKTP.Text = r["KTP"].ToString();
                cbLoaiVL.Text = r["Loaivl"].ToString();
                
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (txtPageNo.Text == "")
            {
                MessageBox.Show("Chưa nhập Page Number");
            }
            //else if (cbCode.SelectedValue.ToString() == "")
            //{
            //    MessageBox.Show("Chưa nhập Code");
            //}
            //else if (cbStaffRegister.SelectedValue.ToString() == "")
            //{
            //    MessageBox.Show("Chưa nhập Staff Register");
            //}
            //else if (cbForm.SelectedValue.ToString() == "")
            //{
            //    MessageBox.Show("Chưa nhập Form");
            //}
            else if (txtThickness.Text == "")
            {
                MessageBox.Show("Chưa nhập Thickness");
            }
            else if (txtWidth.Text == "")
            {
                MessageBox.Show("Chưa nhập Width");
            }
            else if (txtNumber.Text == "")
            {
                MessageBox.Show("Chưa nhập Number");
            }
            //else if (cbConfigureName.SelectedValue.ToString() == "")
            //{
            //    MessageBox.Show("Chưa nhập Config Name");
            //}
            else if (cbKDID.Text == "")
            {
                MessageBox.Show("Chưa nhập KDID");
            }
            else if (txtRessionID.Text == "")
            {
                MessageBox.Show("Chưa nhập Ression");
            }
            else if (txtLenght.Text == "")
            {
                MessageBox.Show("Chưa nhập Lenght");
            }
            else
            {
                try
                {

                    int.Parse(txtNumber.Text);
                    float.Parse(txtThickness.Text);
                    float.Parse(txtLenght.Text);
                    float.Parse(txtWidth.Text);
                    b.ID = int.Parse(ID);
                    b.Loaivl = cbLoaiVL.Text;
                    b.KTP = txtKTP.Text;
                    b.Bafu = checkBafu.Checked;
                    b.Blast30 = checkBlast30.Checked;
                    b.Blast60 = checkBlast60.Checked;
                    b.Casiras = checkCasiras.Checked;
                    b.Code = cbCode.SelectedValue.ToString();
                    if (cbConfigureName.SelectedValue.ToString() == "0")
                    {
                        b.ConfigureName = "";
                    }
                    else
                    {
                        b.ConfigureName = cbConfigureName.SelectedValue.ToString();
                    }
                    b.Cut = checkCut.Checked;
                    b.Date = dateDate.Value;
                    b.Drawn = checkDrawn.Checked;
                    if (cbForm.SelectedValue.ToString() == "0")
                    {
                        b.Form = "";
                    }
                    else
                    {
                        b.Form = cbForm.SelectedValue.ToString();
                    }
                    b.HairLine = checkHairLine.Checked;
                    b.Handl = checkHandl.Checked;
                    b.Helisert = checkHelisert.Checked;
                    b.InLuoi = checkInLuoi.Checked;
                    b.KDID = cbKDID.Text;
                    b.Length = float.Parse(txtLenght.Text);
                    b.MaBong = checkMaBong.Checked;
                    b.MaDen = checkMaDen.Checked;
                    b.MaTrang = checkMaTrang.Checked;
                    b.Migaky = checkMigaky.Checked;
                    b.Number = int.Parse(txtNumber.Text);
                    b.PageNo = txtPageNo.Text;
                    b.PalCoat = checkPalCoat.Checked;
                    b.RessionID = txtRessionID.Text;
                    b.Seal = checkSeal.Checked;
                    b.SerialClose = checkSerialClose.Checked;
                    b.StaffRegister = cbStaffRegister.SelectedValue.ToString();
                    b.Thickness = float.Parse(txtThickness.Text);
                    b.VacuumPacked = checkVacuumPacked.Checked;
                    b.WashSA = checkWashSA.Checked;
                    b.Width = float.Parse(txtWidth.Text);
                    clsmachineDAO.UpdateMachineConfiguration(b);

                    //write Log
                    log.Table = LogConstant.Table.Product.Datasheet;
                    log.Task = LogConstant.Task.Update;
                    log.Description = "Date: " + b.Date + "; KDID: " + b.KDID + "; RessionID: " + b.RessionID + "; ConfigureName: " + b.ConfigureName;
                    log.WriteLog();

                    MessageBox.Show("Đã nhập dữ liệu thành công");
                    Reload(sender, e);
                }
                catch
                {
                    MessageBox.Show("Number, Thickness, Lenght, Width phải là kiểu số");
                }
            }
        }
    }
}